'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview PVP体力相关的接口
 * @author sundbcn@gmail.com (sun debing)
 */

var moment = require('moment');
var constantUtil = require('../util/constantUtil');
var buildType = require('../consts/buildType');
var buildConf = require('../config/buildConf');
var techType = require('../consts/techType');
var unionTechType = require('../consts/unionTechType');
var unionTechService = require('./unionTechService');
var dataApi = require('../libs/dataApi');

exports.getOpenRoadList = function (hero_lv) {
    var road_list = [];
    var lot_id = 1;
    dataApi.pvp_lot_condition.any(function (lot_item) {
        if (lot_item.hero_lvl <= hero_lv) {
            lot_id = lot_item.id;
        } else {
            return true;
        }
    });

    return dataApi.pvp_lot.findById(lot_id);
};

exports.getHarryRes = function (role_model, build_model, tech_model, union_model) {
    var lost_gold_percentage = constantUtil.value('lost_gold_percentage');

    var gold_protect = 0;
    var food_protect = 0;
    var barn_lvl = build_model.getBuildByType(buildType.BARN).getLv();
    var build_conf = buildConf.getBuildLvConf(buildType.BARN, barn_lvl);
    if (!!build_conf) {
        gold_protect = build_conf.gold_protect;
        food_protect = build_conf.food_protect;
    }

    // 科技增加地窖保护金币和粮食
    var tech_protect_gold = tech_model.getEffByTechType(techType.ADD_PROTECT_GOLD);
    var tech_protect_food = tech_model.getEffByTechType(techType.ADD_PROTECT_FOOD);
    var union_add_protect = unionTechService.getUnionTechEff(union_model, unionTechType.UNION_TECH_105);
    if (union_add_protect > 0) {
        tech_protect_gold += union_add_protect;
        tech_protect_food += union_add_protect;
    }

    if (tech_protect_gold > 0) {
        gold_protect = Math.ceil(gold_protect + gold_protect * tech_protect_gold / 100);
    }
    if (tech_protect_food > 0) {
        food_protect = Math.ceil(food_protect + food_protect * tech_protect_food / 100);
    }

    var gold = Math.ceil((role_model.gold - gold_protect) * lost_gold_percentage / 100);
    var food = Math.ceil((role_model.food - food_protect) * lost_gold_percentage / 100);
    if (gold < 0) {
        gold = 0;
    }
    if (food < 0) {
        food = 0;
    }
    return {
        gold: gold,
        food: food
    };
};
